#include <typescommon.h>
#include <stdarg.h>
#include <fcntl.h> // file option
#include <errno.h>

typedef struct
{
    int baseInt;
    int *intPtr;
} shallowStc;

#define chkIntPtrSize (sizeof(int) * 1)

int deepCopyFunc(shallowStc *decs, shallowStc *srcs)
{
    decs->baseInt = srcs->baseInt;
    decs->intPtr = (int *)malloc(chkIntPtrSize);
    memcpy(decs->intPtr, srcs->intPtr, chkIntPtrSize);
    return chkIntPtrSize;
}

int main(int argc, char **argv)
{
    shallowStc tmpsl;
    tmpsl.baseInt = 10;
    tmpsl.intPtr = (int *)malloc(chkIntPtrSize);
    *tmpsl.intPtr = 112;
    logdebug("baseint=%d,intptr ptr=%p, intptr val=%d", tmpsl.baseInt, tmpsl.intPtr, *tmpsl.intPtr);
    {
        shallowStc tmpsl1;
        deepCopyFunc(&tmpsl1, &tmpsl);
        // free(tmpsl.intPtr);此时释放原有指针，深拷贝目标的数据不受影响！
        logdebug("baseint=%d,intptr ptr=%p, intptr val=%d", tmpsl1.baseInt, tmpsl1.intPtr, *tmpsl1.intPtr);
        free(tmpsl1.intPtr);// 记得释放。
    }
    {
        shallowStc tmpsl1;
        tmpsl1 = tmpsl;
        // free(tmpsl.intPtr);此时释放原有指针，浅拷贝目标的数据也被释放，会有问题！
        logdebug("baseint=%d,intptr ptr=%p, intptr val=%d", tmpsl1.baseInt, tmpsl1.intPtr, *tmpsl1.intPtr);
    }
    free(tmpsl.intPtr);//释放原指针。
    return 1;
}
